/**
 * 
 */
package org.ikongfuzi.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;

import org.ikongfuzi.ReaderRSS;
import org.ikongfuzi.common.PMF;
import org.ikongfuzi.common.Rssinfo;
import org.ikongfuzi.common.Userinfo;

/**
 * @author kelch
 *
 */
public class UserManager {
	
	private static PersistenceManagerFactory pmf =  PMF.get();

	/**
	 * 
	 */
	public UserManager() {
		// TODO Auto-generated constructor stub
	}
	
    public Userinfo adduser(Map <String,String>uInfo){
    	PersistenceManager pm = null;
    	Userinfo user = null;
		try {
			pm = pmf.getPersistenceManager();
			String nickName = uInfo.get("nickName").trim();
			String passWord = uInfo.get("passWord").trim();
			String eMail = uInfo.get("email").trim();
			int exi = checkNickName(nickName);
			int exie = checkEMail(eMail);
			if(exi==0&&exie==0){//不存在，可添加
				user = new Userinfo(nickName,passWord,eMail);
				user = pm.makePersistent(user);
				return user;//成功
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			pm.close();
		}
    	return user;
    }
    /**
     * 
     * 描述:
     * @param username
     * @return
     * @author kelch
     * 2010-11-23
     */
    public int checkNickName(String nickName){
    	PersistenceManager pm = null;
		try {
			pm = pmf.getPersistenceManager();
			Query query = pm.newQuery(Userinfo.class);
			query.setFilter("nickName==para");
			query.declareParameters("String para");
			List<Userinfo> users = (List<Userinfo>)query.execute(nickName);
			if(users==null||users.size()<1){
				return 0;//不存在
			}
			return 1;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			pm.close();
		}
    	return -1;
    }
    /**
     * 
     * 描述:
     * @param eMail
     * @return
     * @author kelch
     * 2010-11-23
     */
    public int checkEMail(String email){
    	PersistenceManager pm = null;
		try {
			pm = pmf.getPersistenceManager();
			Query query = pm.newQuery(Userinfo.class);
			query.setFilter("email==para");
			query.declareParameters("String para");
//			query.setResultClass(Userinfo.class);
			List<Userinfo> users = (List<Userinfo>)query.execute(email);
			if(users==null||users.size()<1){
				return 0;//不存在
			}
			return 1;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			pm.close();
		}
    	return -1;
    }
    /**
     * 
     * 描述:添加RSS频道，跟用户关联
     * @param userId
     * @param chalId
     * @return
     * @author kelch
     * 2010-11-24
     */
    public int addChannel(Long userId,Long chalId){
    	PersistenceManager pm = null;
		try {
			pm = PMF.get().getPersistenceManager();
			Userinfo user = pm.getObjectById(Userinfo.class,userId);
			if(user!=null){
				List<Long> chals = user.getChannels();
				if(chals==null){
					chals = new ArrayList<Long>();
				}
				chals.remove(chalId);
				chals.add(chalId);
			    user.setChannels(chals);
				pm.makePersistent(user);
				return 0;
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			pm.close();
		}
    	return -1;
    }
    
    /**
     * 
     * 描述:删除RSS频道，跟用户关联
     * @param userId
     * @param chalId
     * @return
     * @author kelch
     * 2010-11-24
     */
    public int delChannel(Long userId,Long chalId){
    	PersistenceManager pm = null;
		try {
			pm = PMF.get().getPersistenceManager();
			Userinfo user = pm.getObjectById(Userinfo.class,userId);
			if(user!=null){
				List<Long> chals = user.getChannels();
				if(chals==null){
					chals = new ArrayList<Long>();
				}
				chals.remove(chalId);
			    user.setChannels(chals);
				pm.makePersistent(user);
				return 0;
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			pm.close();
		}
    	return -1;
    }
    /**
     * 
     * 描述:取得用户自定义的频道
     * @param userId
     * @return
     * @author kelch
     * 2010-11-24
     */
    public List<Rssinfo> getUserRss(Long userId){
    	PersistenceManager pm = null;
    	List <Rssinfo> rssLis = new ArrayList<Rssinfo>();
		try {
			pm = PMF.get().getPersistenceManager();
			Userinfo user = pm.getObjectById(Userinfo.class,userId);
			if(user!=null){
				List<Long> chals = user.getChannels();
				Iterator<Long> itr = chals.iterator();
				Rssinfo rss = null;
				while(itr.hasNext()){
					rss = pm.getObjectById(Rssinfo.class, itr.next());
					rssLis.add(rss);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			pm.close();
		}
    	return rssLis;
    }
    public List<Rssinfo> getUserRssUnOrder(Long userId){
    	PersistenceManager pm = null;
//    	List <Rssinfo> rssLis = new ArrayList<Rssinfo>();
    	List<Rssinfo> allRss =  new ReaderRSS().getRssinfoByName();
    	//Iterator<Rssinfo> itr = allRss.iterator();
		try {
			pm = PMF.get().getPersistenceManager();
			Userinfo user = pm.getObjectById(Userinfo.class,userId);
			if(user!=null){
				List<Long> chals = user.getChannels();
				Iterator<Long> itr = chals.iterator();
				Rssinfo rss = null;
				while(itr.hasNext()){
					
//					rss = pm.getObjectById(Rssinfo.class, itr.next());
					rss = new Rssinfo();
					rss.setId(itr.next());
					allRss.remove(rss);
//					rssLis.add(rss);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			pm.close();
		}
    	return allRss;
    }
    public List<Userinfo> getAllUser() {
		PersistenceManager pm = null;
		List<Userinfo> userinfos = null;
		try {
			pm = PMF.get().getPersistenceManager();
			Query query = pm.newQuery(Userinfo.class);
			// query.setResultClass(Rssinfo.class);
			userinfos = (List<Userinfo>) query.execute();
			if (userinfos != null)
				userinfos.size();// appEngine的bug，需要操作下返回结果。
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
		return userinfos;
	}
    
    public Userinfo loginSys(String email,String passwd){
    	Userinfo user = null;
    	PersistenceManager pm = null;
		List<Userinfo> lis = null;
		try {
			pm = PMF.get().getPersistenceManager();
			Query query = pm.newQuery(Userinfo.class);
			query.setFilter("email==emailPara && passWord==pwPara");
			query.declareParameters("String emailPara,String pwPara");
			Map map = new HashMap();
			map.put("emailPara", email.trim());
			map.put("pwPara", passwd.trim());
			lis = (List<Userinfo>) query.executeWithMap(map);
			if (lis != null&&lis.size()>0){
				lis.size();// appEngine的bug，需要操作下返回结果。
				user = lis.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
    	return user;
    }

}
